package java2020spring;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Stack;
import java.lang.Math;
public class CalFrame extends JFrame {

    private String cen[]={"+","-","*","÷","7","8","9","sin","4","5","6","cos","1","2","3","tan",".","0","√","1/x"};
    private String wes[]={"MC","MR","MS","M+","M-"};
    private String eas[]={"C","back","x^2","x^3","="};
    JPanel panel = new JPanel();	
    String str=" ";
    TextArea textNum;
    Double result;
    Double store=0.0;
    int flag=0;
    public CalFrame() {
        this.setTitle("Calculator");
         this.setBounds(10,10,400,400);
         this.setResizable(false);
        textNum=new TextArea();
        textNum.setPreferredSize(new Dimension(400,80));
        panel.setLayout(new BorderLayout(10,3));
		panel.add(textNum, BorderLayout.NORTH);
        panel.setPreferredSize(new Dimension(400,400 ));         //文本框
        

        JPanel panelWe=new JPanel();
        panelWe.setLayout(new GridLayout(5,1,0,0));
        JButton[] weButton=new JButton[wes.length];
        for(int i=0;i<wes.length;i++) {
              JButton n=new JButton(wes[i]);  
              Font f=new Font("Arial",Font.BOLD,16);
              n.setFont(f);        
              n.addActionListener(getActionListener());
              n.setForeground(Color.black);
              weButton[i]=n;
        }
        for(JButton n:weButton) {
              panelWe.add(n);                               
        }
        panel.add(panelWe,BorderLayout.WEST);                       //west 布局及按钮设置
        

        JPanel panelEast=new JPanel();
        panelEast.setLayout(new GridLayout(5,1,0,0));
        JButton[] eaButton=new JButton[eas.length];
        for(int i=0;i<eas.length;i++) {
              JButton n=new JButton(eas[i]); 
              Font f=new Font("Arial",Font.BOLD,16);
              n.setFont(f);                 
              n.addActionListener(getActionListener());
              n.setForeground(Color.black);
              eaButton[i]=n;
        }
        for(JButton n:eaButton) {
              panelEast.add(n);                               
        }
        panel.add(panelEast,BorderLayout.EAST);                         //east布局及按钮设置


        JPanel panelCen=new JPanel();
        panelCen.setLayout(new GridLayout(5,4,0,0));
        JButton[] cenButton=new JButton[cen.length];
        for(int i=0;i<cen.length;i++) {
              JButton n=new JButton(cen[i]);   
              Font f=new Font("Arial",Font.BOLD,16); 
              n.setFont(f);              
              n.addActionListener(getActionListener());
              n.setForeground(Color.black);
              cenButton[i]=n;
        }
        for(JButton n:cenButton) {
              panelCen.add(n);                               
        }
        panel.add(panelCen,BorderLayout.CENTER);                     //center 布局和按钮设置



        this.add(panel);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    private ActionListener getActionListener() {
        ActionListener actionListener = new ActionListener() {
        
            @Override
            public void actionPerformed(ActionEvent e) {
                  String action = e.getActionCommand();
                  if(action.equals("C")) {                                              //按下C键事件处理

                        str=" ";                
                        textNum.setText(str);

                  }
                  else if(action.equals("back")) {                                //按退格键事件处理
                    if(str==" ")
                    {
                    }
                    else
                    {
                        str=str.substring(0,str.length()-1);
                        textNum.setText(str);
                    }

                  }
                  else if(action.equals("=")) {                                   //按等于键时间处理
                    if(str==" ")
                    {
                    }
                    else
                    {
                      String [] act=str.split(" ");   
                      String preAct=act[act.length-1];
                      if(preAct.equals("+")||preAct.equals("-")||preAct.equals("*")||preAct.equals("÷"))
                      {
                         textNum.setText(" 输入错误");
                         str=" ";
                      }
                      else
                      {   
                         textNum.setText(calculate.cal(str));
                         str=calculate.cal(str);
                      }
                    }
                  }
                  else if(action.equals("MC")||action.equals("MR")||action.equals("MS")||action.equals("M+")||action.equals("M-")) {                //MC,MR,MS,M+按键的处理 
                        
                         if(action.equals("MC"))                                   //清空存储数据
                         {
                              store=0.0;
                         }
                         if(action.equals("MR"))                                   //输出存储数据
                         {               
                              if(flag!=0)
                              {
                                 String sstore=String.valueOf(store);
                                 textNum.setText(" "+sstore);
                              }
                              if(flag==0)
                              {
                                 textNum.setText("暂无存储内容，请输入MS开始存储");
                              }
                         }
                         if(action.equals("MS"))                                    //开始存储当前数据
                         {
                              if(str==" ")
                              {
                                     textNum.setText("请输入数字");
                              }
                              if(str!=" ")
                              {
                                    String sstore1=str;
                                    Double store1=Double.parseDouble(sstore1);
                                    store=store1;
                                    str=" ";
                                    textNum.setText(" ");
                                    flag=1;
                              }
                         }
                         if(action.equals("M+"))                                     //将当前数据加入存储数据中
                         {                 
                                String [] act=str.split(" ");
                                if(str!=" "&&act.length==1&&flag==1)
                                {
                                     String sstore1=str;
                                     Double store1=Double.parseDouble(sstore1);
                                     store+=store1;
                                     str=" ";
                                     textNum.setText(" ");
                                     flag=1;
                                 }
                                 else
                                 {
                                      textNum.setText("由于目前暂未开始存储数据或表达式未计算\r\n请将表达式计算完毕进行存储或输入数据按MS开始存储");
                                 }
                         }
                          if(action.equals("M-"))                                       //将存储的数据减去当前数据
                         {
                              String [] act=str.split(" ");
                              if(str!=" "&&act.length==1&&flag==1)
                              {
                                   String sstore1=str;
                                   Double store1=Double.parseDouble(sstore1);
                                   store-=store1;
                                   str=" ";
                                   flag=1;
                               }
                               else
                               {
                                    textNum.setText("由于目前暂未开始存储数据或表达式未计算\r\n请将表达式计算完毕进行存储或输入数据按MS开始存储");
                               }
                         }



                  }
                  else if(action.equals("√")||action.equals("1/x")||action.equals("x^2")||action.equals("x^3")) {                      //1/x键,√键,x^2键,x^3键的处理

                  if(str==" ")
                  {
                      textNum.setText(" 请输入数字后再进行该操作！");
                       str=" ";
                  }
                  else
                  {
                      if(action.equals("√"))
                      { 
                        Double res2;
                        String sres1=(calculate.cal(str));
                        Double res1=Double.parseDouble(sres1);
                        res2=Math.sqrt(res1);
                        String sres2=String.valueOf(res2);
                        textNum.setText(" "+sres2);
                        str=" "+sres2; 
                      }
                      else if(action.equals("1/x"))
                      {
                        Double res2;
                        String sres1=(calculate.cal(str));
                        Double res1=Double.parseDouble(sres1);
                        res2=Math.pow(res1,-1);
                        String sres2=String.valueOf(res2);
                        textNum.setText(" "+sres2);
                        str=" "+sres2; 
                      }
                      else if(action.equals("x^2"))
                      {
                        Double res2;
                        String sres1=(calculate.cal(str));
                        Double res1=Double.parseDouble(sres1);
                        res2=res1*res1;
                        String sres2=String.valueOf(res2);
                        textNum.setText(" "+sres2);
                        str=" "+sres2; 
                      }
                      else if(action.equals("x^3"))
                      {
                        Double res2;
                        String sres1=(calculate.cal(str));
                        Double res1=Double.parseDouble(sres1);
                        res2=res1*res1*res1;
                        String sres2=String.valueOf(res2);
                        textNum.setText(" "+sres2);
                        str=" "+sres2; 
                      }
                    }
                  }
                   else if(action.equals("sin")||action.equals("cos")||action.equals("tan")) {                      //sin键,cos键,tan键的处理
                    if(str==" ")
                    {
                       textNum.setText(" 请输入数字后再进行该操作！");
                       str=" ";
                    }
                    else
                    {
                      if(action.equals("sin"))
                      { 
                        Double res2;
                        String sres1=(calculate.cal(str));
                        Double res1=Double.parseDouble(sres1);
                        res2=Math.sin(res1);
                        String sres2=String.valueOf(res2);
                        textNum.setText(" "+sres2);
                        str=" "; 
                      }
                      else if(action.equals("cos"))
                      {
                        Double res2;
                        String sres1=(calculate.cal(str));
                        Double res1=Double.parseDouble(sres1);
                        res2=Math.cos(res1);
                        String sres2=String.valueOf(res2);
                        textNum.setText(" "+sres2);
                        str=" "; 
                      }
                      else if(action.equals("tan"))
                      {
                        Double res2;
                        String sres1=(calculate.cal(str));
                        Double res1=Double.parseDouble(sres1);
                        res2=Math.tan(res1);
                        String sres2=String.valueOf(res2);
                        textNum.setText(" "+sres2);
                        str=" "; 
                      }
                    }
                  }
                  else if(action.equals("+")||action.equals("-")||action.equals("*")||action.equals("÷"))  {              //加减乘除输入处理
                    
                        String [] act=str.split(" ");   
                        String preAct=act[act.length-1];
                        if(preAct.equals("+")||preAct.equals("-")||preAct.equals("*")||preAct.equals("÷")) 
                        {
                         }  
                        else
                        {
                               str=str+" "+action+" ";
                               textNum.setText(str);
                        }

                  }
                  else {                                                    //数字的输入

                           str+=action;
                           textNum.setText(str);

                  }

   
            }
        };
        return actionListener;
    }
    public static void main(String[ ] args) {

		new CalFrame();

    }
  
}


class calculate {                                                                               //加减乘除运算方法
      public static String cal(String str)  {
           int t=0;
           String [] act=str.split(" ");                                          //将字符串按照数字与运算符分开
           Stack<Double> stack=new Stack<Double>();
           stack.push(Double.parseDouble(act[1])); //将数字转化为double型
           for(int i=2;i<act.length;i++) {
               if(i%2==0) {
                     if(act[i].equals("+")) {
                              stack.push(Double.parseDouble(act[i+1]));
                     }
                     if(act[i].equals("-")) {
                              stack.push(-Double.parseDouble(act[i+1]));
                     }
                     if(act[i].equals("*")) {                                          //将乘除法先计算后再进行存储
                              Double NumOne=stack.pop();
                              Double NumTwo=Double.parseDouble(act[i+1]);
                              stack.push(NumOne*NumTwo);
                     }
                      if(act[i].equals("÷")) {
                              Double NumOne=stack.pop();
                              Double NumTwo=Double.parseDouble(act[i+1]);
                              if(NumOne!=0)
                              {
                                  stack.push(NumOne/NumTwo);
                              }
                              else
                              {
                                  t=1;
                                  break;
                              }                                  
                     }
               }
           }
           double sum=0;
           if(t==0)
           {
             while(!stack.isEmpty())                                             //将存储的数据相加
             {
                 sum=sum+stack.pop();

             }
             String str2=String.valueOf(sum);
             return " "+str2;
          }
          else
          {
              return "出现被除数为0的错误，请重新输入";
          }
      }

}
